package com.beta.password_menu;

import java.util.ArrayList;
import java.util.List;

import com.beta.nfc_beta.R;
import com.beta.nfc_beta.vars.Vars;
import com.beta.password_menu.LockPatternView.Cell;


import android.app.Activity;
import android.app.ActivityManager;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class SetPasswordActivity extends Activity{
	private SharedPreferences AppIni;
	private String password;
	private int patternSize;
	private Intent i;
	private Button btnSpLeft, btnSpRight;
	private TextView TopText, BottomText;
	private LockPatternView mLockPatternView;
	private ArrayList<Cell> mPattern;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.setpassword);

		btnSpLeft = (Button) findViewById(R.id.btnSpLeft);
		btnSpRight = (Button) findViewById(R.id.btnSpRight);
		TopText = (TextView) findViewById(R.id.SpTopText);
		BottomText = (TextView) findViewById(R.id.SpBottomText);
		mLockPatternView = (LockPatternView) findViewById(R.id.SpLockPattern);

		AppIni = getSharedPreferences("configure", Context.MODE_WORLD_WRITEABLE
				| Context.MODE_WORLD_READABLE);
		boolean FirstTimeOpenApp = AppIni.getBoolean("FirstTimeOpenApp", true);// ���缍�����娑�����锟筋��╂�������缁��绨�
		if (FirstTimeOpenApp) {
			btnSpLeft.setText(R.string.ok);
			btnSpRight.setText(R.string.exit);
			TopText.setText(R.string.firsttime);
			BottomText.setText(R.string.welcome);
		}

		btnSpLeft.setOnClickListener(new Button.OnClickListener() {
			public void onClick(View arg0) {
				if(patternSize>=4){
				AppIni.edit().putString("Password", password).commit();
				Log.v("msg", "jiesuo:" + AppIni.getString("Password", null));
				AppIni.edit().putBoolean("FirstTimeOpenApp", false).commit();
				SetPasswordActivity.this.finish();
				}
			}
		});
		btnSpRight.setOnClickListener(new Button.OnClickListener() {
			public void onClick(View arg0) {
				boolean FirstTimeOpenApp = AppIni.getBoolean("FirstTimeOpenApp",
						true);
				if (FirstTimeOpenApp) {
					AppIni.edit().putBoolean("FirstTimeOpenApp", true).commit();
					i = SetPasswordActivity.this.getIntent();
					SetPasswordActivity.this.setResult(
							Vars.RESULTFROMSETPASSWORD, i);
					SetPasswordActivity.this.finish();
				} else {
					SetPasswordActivity.this.finish();
				}
			}
		});
		mLockPatternView
				.setOnPatternListener(new LockPatternView.OnPatternListener() {
					public void onPatternStart() {
						TopText.setText(R.string.pattern_start);
					}

					public void onPatternCleared() {
						Log.v("msg", "Cleared");
					}

					public void onPatternCellAdded(List<Cell> pattern) {
					}

					public void onPatternDetected(List<Cell> pattern) {
						patternSize=pattern.size();
						if (pattern.size() >= 4) {
							TopText.setText(R.string.pattern_end);
							mLockPatternView
									.setDisplayMode(LockPatternView.DisplayMode.Correct);
							mPattern = (ArrayList<Cell>) pattern;
							password = LockPatternUtils
									.patternToString(pattern);
						} else {
							TopText.setText(R.string.patternlimit);
							mLockPatternView
									.setDisplayMode(LockPatternView.DisplayMode.Wrong);
						}
					}
				});
	}

	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			boolean FirstTimeOpenApp = AppIni.getBoolean("FirstTimeOpenApp",
					true);
			if (FirstTimeOpenApp) {
				AppIni.edit().putBoolean("FirstTimeOpenApp", true).commit();
				i = SetPasswordActivity.this.getIntent();
				SetPasswordActivity.this.setResult(
						Vars.RESULTFROMSETPASSWORD, i);
				SetPasswordActivity.this.finish();
			} else {
				SetPasswordActivity.this.finish();
			}
		}
		return super.onKeyDown(keyCode, event);
	}
}
